﻿using Finance.Infrastructrue.Interface;
using Finance.API.Write.Application.Command.FinancialCategoryCommand;
using Finance.Domain;
using Finance.ErrorCode;
using MediatR;
using SqlSugar;
using Org.BouncyCastle.Ocsp;
using Finance.Infrastructrue.Implement;

namespace Finance.API.Write.Application.Hander.FinancialCategoryHander
{
    public class DelFinancialCategoryHander:IRequestHandler<DelFinancialCategoryCommand,APIResult<int>>
    {
       
        DatabaseContext Db;
        IBaseRepository<FinancialCategory> finRep;
        ILogger<DelFinancialCategoryHander> logger;

        public DelFinancialCategoryHander(DatabaseContext db, IBaseRepository<FinancialCategory> finRep, ILogger<DelFinancialCategoryHander> logger)
        {
            Db = db;
            this.finRep = finRep;
            this.logger = logger;
        }

        public   async Task<APIResult<int>> Handle(DelFinancialCategoryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> res = new APIResult<int>();
               var m= Db.SqlSugar.Queryable<FinancialCategory>().First(a => a.FinancialCategoryId == request.financialCategoryId);
                m.IsDel= true;  
                int i= await Db.SqlSugar.Updateable(m).ExecuteCommandAsync();

                res.Code = APIEnum.Ok;
                res.Message = "财务数据财务类别逻辑删除成功";
                res.Data = i;
                
                return await Task.FromResult(res);
            }
            catch (Exception ex)
            {
                logger.LogError("财务数据财务类别逻辑删除失败" + ex.Message);
                throw;
            }
        }
    }
}
